﻿var ReportTypeData=[{name:"网格",code:"0"},{name:"交叉",code:"1"},{name:"参数",code:"2"}];
var UITypeData=[{name:"hidden",code:"0"},{name:"Text",code:"1"},{name:"DateTime",code:"2"},{name:"Date",code:"4"},{name:"Int",code:"5"},{name:"Select",code:"7"}];

//初始化入口
$(function() {
    fn_Tree();
    $('#dlg-addClass').dialog('close');
    $('#dlg-addTable').dialog('close');
    $('#dlg-editField').dialog('close');
    $('#dlg-showunitdata').dialog('close');
    $('#formtabletype').combobox('loadData', ReportTypeData);
    $('#fieldUIType').combobox('loadData', UITypeData);
    $('#fieldUIType').combobox('options').onSelect = function(record) {
        if (record.name == "Select") {
            $('#fieldDataUnitId').combobox('enable');
            $('#fieldDynamicSQL').removeAttr('readonly');

        } else {
            $('#fieldDataUnitId').combobox('disable');
            $('#fieldDynamicSQL').attr('readonly', 'true');
        }
    };
    $('#fieldDataUnitId').combobox('options').onSelect = function(record) {
        $('#fieldDynamicSQL').val(record.UnitSQL);
    };
});

function fn_Tree() {
    $('#treeload').tree({
        url: "Controller.aspx?controller=ReportController&method=LoadLayers",
        onClick: function(node) {
            $(this).tree('toggle', node.target);
            if (parseInt(node.attributes.type) == 1) {
                $('#fieldDataUnitId').combobox({ url: "Controller.aspx?controller=GeneralController&method=GetDataUnit" });
                $('#fieldgrid').datagrid('options').url = "Controller.aspx?controller=ReportController&method=QueryFieldList&titleId=" + node.id;
                $('#fieldgrid').datagrid('reload');
                $('#btn_searchReport').linkbutton('enable');
                $('#btn_searchData').linkbutton('enable');
                //printDivPreview($('#printDiv'));
            } else {
                $('#btn_searchReport').linkbutton('disable');
                $('#btn_searchData').linkbutton('disable');
                $('#printDiv').empty();
            }
        }
    });
}

//添加分类
function btn_AddClass(){
    var selected=$('#treeload').tree('getSelected');
    if(selected){
        if(parseInt(selected.attributes.type)==0){
            $("#formaddclass").form('clear');
            $("#formGeneralLayerId").val(0);
            $("#formPGeneralLayerId").val(selected.id);
            $('#dlg-addClass').dialog({title:'添加分类'});
        }
    }
}
//确认添加分类
function dlg_addClassOk(){
    FormSubmit($('#formaddclass'), 'Controller.aspx?controller=ReportController&method=AddGeneralLayer', function() {
                    $('#dlg-addClass').dialog('close');
                    $('#treeload').tree('reload');
                });
}

//添加报表
function btn_AddTable(){
    var selected=$('#treeload').tree('getSelected');
    if(selected){
        if(parseInt(selected.attributes.type)==0){
            $("#formaddtable").form('clear');
            $("#formtitileId").val(0);
            $("#formPLayerId").val(selected.id);
            $('#dlg-addTable').dialog({title:'添加报表'});
        }
    }
}

function dlg_addTableOk(){
    FormSubmit($('#formaddtable'), 'Controller.aspx?controller=ReportController&method=AddReportTitle', function() {
                    $('#dlg-addTable').dialog('close');
                    $('#treeload').tree('reload');
                });
}

function btn_AlterTree(){
    var selected=$('#treeload').tree('getSelected');
    if(selected){
        if(parseInt(selected.id)==-1)return false;
        if(parseInt(selected.attributes.type)==0){
            $("#formaddclass").form('clear');
            $("#formGeneralLayerId").val(selected.id);
            $("#formPGeneralLayerId").val(selected.attributes.PLayerId);
            $("#formclassname").val(selected.text);
            $('#dlg-addClass').dialog({title:'修改分类'});
        }else{
            $("#formaddtable").form('clear');
            $("#formtitileId").val(selected.id);
            $("#formPLayerId").val(selected.attributes.LayerId);
            $("#formtablename").val(selected.text);
            $("#formReportFileName").val(selected.attributes.ReportFileName);
            $("#formtabletable").combobox('setValue',selected.attributes.ProcedureName);
            $("#formtabletype").combobox('setValue',selected.attributes.ReportType);
            $("#formtablememo").val(selected.attributes.Memo);
            $('#dlg-addTable').dialog({title:'修改报表'});
        }
    }
}

function btn_DelTree(){
    var selected=$('#treeload').tree('getSelected');
    if(selected){
        if(parseInt(selected.id)==-1)return false;
        if(parseInt(selected.attributes.type)==0){
            $.messager.confirm('询问', '是否要删除此记录？', function(r){
				if (r){
					 requestAjax('Controller.aspx?controller=ReportController&method=DeleteGeneralLayer', { GeneralLayerId: selected.id }, function(data) {
                            $('#treeload').tree('reload');
                        });
				}
			});
        }else{
            $.messager.confirm('询问', '是否要删除此记录？', function(r){
				if (r){
					 requestAjax('Controller.aspx?controller=ReportController&method=DeleteReportTitle', { TitleId: selected.id }, function(data) {
                            $('#treeload').tree('reload');
                        });
				}
			});
        }
    }
}

//初始化参数
function btngrid_onInitField(){
    var selected=$('#treeload').tree('getSelected');
    if(selected){
        if(parseInt(selected.attributes.type)==1){
            $.messager.confirm('询问', '初始化会删除现有所有数据，确定要初始化吗？', function(r){
				if (r){
					 requestAjax('Controller.aspx?controller=ReportController&method=InitField', { TitleId: selected.id,ProcedureName:selected.attributes.ProcedureName }, function(data) {
                            $('#fieldgrid').datagrid('options').url = "Controller.aspx?controller=ReportController&method=QueryFieldList&titleId="+selected.id;
                            $('#fieldgrid').datagrid('reload');
                        });
				}
			});
		}
    }
}

//编辑参数
function btngrid_onEditField(){
    var selected=$('#fieldgrid').datagrid('getSelected');
    if(selected){
        $('#formeditfield').form('load',selected);
        
        if(selected.UIType=="7"){
            $('#fieldDataUnitId').combotree('enable');
            $('#fieldDynamicSQL').removeAttr('readonly');
        }else{    
            $('#fieldDataUnitId').combotree('disable');
            $('#fieldDynamicSQL').attr('readonly','true');
        }
            
        $('#dlg-editField').dialog('open');
    }
}

function dlg_editFieldOk(){
     FormSubmit($('#formeditfield'), 'Controller.aspx?controller=ReportController&method=UpdateReportField', function() {
                    $('#dlg-editField').dialog('close');
                    $('#fieldgrid').datagrid('reload');
                });
}
//报表查询
function onStartSearchData(){
    var selected=$('#treeload').tree('getSelected');
    if(selected){
        requestAjax('Controller.aspx?controller=ReportController&method=LoadPageReportSearch&titleId=' + selected.id, {}, function(data) {
                $('#addDataDiv').empty();
                $('#addDataDiv').load("ReportSearchdlg.aspx",{},function(){
                    $.parser.parse($('#addDataDiv'));
                 });
            });
        $('#dlgresult-addData').dialog('open'); 
    }
}
//查看报表数据源 方便设计报表时字段对应
function onStartReportDS() {
    var selected = $('#treeload').tree('getSelected');
    if (selected) {
        requestAjax('Controller.aspx?controller=ReportController&method=GetReportDataSource&titleId=' + selected.id, {}, function(data) {
            $('#dlgresult-searchDataSource').dialog('open');
            $('#dataSourceDiv').empty();
            $('#dataSourceDiv').load("ReportSearchDataSource.aspx", {}, function() {
                $.parser.parse($('#dataSourceDiv'));
            });
        });
        
    }
}

//显示单元数据
function ShowUnitData() {
    var selected = $('#fieldDataUnitId').combobox('getValue');
    if (selected) {
        $('#dlg-showunitdata').dialog('open');
        $('#showunitGrid').datagrid('options').url = "Controller.aspx?controller=GeneralController&method=GetUnitData&dataunitId=" + selected + "&unitSql=" + $('#fieldDynamicSQL').val();
        $('#showunitGrid').datagrid('reload');
    }
}


//报表查询确定
function dlgsearch_reportOk(){
    var selected=$('#treeload').tree('getSelected');
    if(selected){
        FormSubmit($('#formreportsearch'), 'Controller.aspx?controller=ReportController&method=ExecProcedure&titleId='+selected.id, function() {
                    $('#dlgresult-addData').dialog('close');
                    printDivPreview($('#printDiv'));
                });
           }
    
}

function UITypeFormatter(value){
    for(var i=0; i<UITypeData.length; i++){
		if (UITypeData[i].code == value) return UITypeData[i].name;
	}
	return value;
}